const Nedb = require('nedb')
const path = require('path')

const bookDB = new Nedb({
  filename: path.join('.', 'data', 'book01.nedb'),
  autoload: true
})

const taskDB = new Nedb({
  filename: path.join('.', 'data', 'task01.nedb'),
  autoload: true
})

async function executeWithPromiseReturned (fn, fnThis, ...args) {
  if (typeof fn !== 'function') throw new TypeError('the first argument must be a function')
  return new Promise((resolve, reject) => {
    fn.call(fnThis, ...args, (err, ret) => {
      if (err) reject(err)
      else resolve(ret)
    })
  })
}

module.exports = {
  bookDB,
  taskDB,
  executeWithPromiseReturned
}
